package com.drug.mingkang.service.impl;

import com.drug.mingkang.mapper.HqInventoryMapper;
import com.drug.mingkang.mapper.HqWarehousingMapper;
import com.drug.mingkang.model.HqInventory;
import com.drug.mingkang.model.vo.*;
import com.drug.mingkang.model.vo.DrugInventoryDetailVo;
import com.drug.mingkang.model.vo.InventoryVo;
import com.drug.mingkang.model.vo.MaterialInventoryDetailVo;
import com.drug.mingkang.model.vo.MaterialInventoryVo;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.HqInventoryService;
import com.drug.mingkang.utils.EarlyWarning;
import com.drug.mingkang.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 总公司库存表(HqInventory)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@Service("hqInventoryService")
public class HqInventoryServiceImpl implements HqInventoryService {
    @Resource
    private HqInventoryMapper hqInventoryMapper;

    @Resource
    private Tools tools;

    @Override
    public int findCount(HttpSession session) {
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        if ("0".equals(perUsers.getCompanyId())){
            return this.hqInventoryMapper.findCount(EarlyWarning.ZGS_YPKCYJ,EarlyWarning.ZGS_YCLKCYJSL);
        }else {
            return this.hqInventoryMapper.findCounts(perUsers.getCompanyId(),EarlyWarning.FGS_YPKCYJ);
        }
    }

    @Resource
    private HqWarehousingMapper hqWarehousingMapper;

    @Override
    public List<PurchaseMaterialVo> findMaterialsWarning(PurchaseMaterialVo vo,int num) {
        return this.hqInventoryMapper.findMaterialsWarning(vo,num);
    }

    @Override
    public int findMaterialsWarningCount(PurchaseMaterialVo vo,int num) {
        return this.hqInventoryMapper.findMaterialsWarningCount(vo,num);
    }

    @Override
    public List<DrugInventoryDetailVo> findDrugDetails(DrugInventoryDetailVo vo,String id) {
        if ("0".equals(id)){
            return this.hqInventoryMapper.findDrugDetails(vo);
        }else {
            return this.hqInventoryMapper.findDrugDetailsA(vo,id);
        }

    }

    @Override
    public int findDrugDetailsCount(DrugInventoryDetailVo vo,String id) {
        if ("0".equals(id)){
            return this.hqInventoryMapper.findDrugDetailsCount(vo);
        }else {
            return this.hqInventoryMapper.findDrugDetailsCountA(vo,id);
        }
    }

    @Override
    public Map<String, Object> findAllDrugEarlyWarning(int zgsYpkcyj, String companyId, int page, int limit) {
        return tools.limit(this.hqInventoryMapper.findAllDrugEarlyWarning(zgsYpkcyj, companyId, (page - 1) * limit, limit), this.hqInventoryMapper.findAllDrugEarlyWarningCount(zgsYpkcyj, companyId));
    }

    @Override
    public List<MaterialInventoryDetailVo> findMaterialsDetails(MaterialInventoryDetailVo vo) {
        return this.hqInventoryMapper.findMaterialsDetails(vo);
    }

    @Override
    public int findMaterialsDetailsCount(MaterialInventoryDetailVo vo) {
        return this.hqInventoryMapper.findMaterialsDetailsCount(vo);
    }

    @Override
    public Map<String, Object> showDrugInventoryAll(InventoryVo inventoryVo) {
        return tools.limit(this.hqInventoryMapper.queryAllByLimit(inventoryVo), this.hqInventoryMapper.count(inventoryVo));
    }

    /**
     * 根据药品id查看出入库明细
     * @param id  库存id
     * @param page  页
     * @param limit 条
     * @return
     */
    @Override
    public Map<String,Object> findByRepertoryId(String id, int page, int limit) {
        return tools.limit(hqWarehousingMapper.findByRepertoryId(id, (page - 1) * limit, limit),hqWarehousingMapper.count(id));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param inventoryId 主键
     * @return 实例对象
     */
    @Override
    public HqInventory queryById(String inventoryId) {
        return this.hqInventoryMapper.queryById(inventoryId);
    }

    @Override
    public HqInventory queryByDrugId(String drugId) {
        return this.hqInventoryMapper.queryByDrugId(drugId);
    }

    @Override
    public int updateByDrug(List<HqInventory> list) {
        return this.hqInventoryMapper.updateByDrug(list);
    }

    @Override
    public HqInventory queryByMaterialId(String materialId) {
        return this.hqInventoryMapper.queryByMaterialId(materialId);
    }


    /**
     * 新增数据
     *
     * @param hqInventory 实例对象
     * @return 实例对象
     */
    @Override
    public HqInventory insert(HqInventory hqInventory) {
        this.hqInventoryMapper.insert(hqInventory);
        return hqInventory;
    }

    @Override
    public List<HqInventory> queryAll() {
        return this.hqInventoryMapper.queryAll();
    }


    /**
     * 修改数据
     *
     * @param hqInventory 实例对象
     * @return 实例对象
     */
    @Override
    public HqInventory update(HqInventory hqInventory) {
        this.hqInventoryMapper.update(hqInventory);
        return hqInventory;
    }

    @Override
    public HqInventory selectMaterialId(String materialId) {
        return this.hqInventoryMapper.selectMaterialId(materialId);
    }

    @Override
    public List<MaterialInventoryVo> selectMaterialAll(MaterialInventoryVo vo) {
        return this.hqInventoryMapper.selectMaterialAll(vo);
    }

    @Override
    public int selectMaterialCount(MaterialInventoryVo vo) {
        return this.hqInventoryMapper.selectMaterialCount(vo);
    }



    /**
     * 通过主键删除数据
     *
     * @param inventoryId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String inventoryId) {
        return this.hqInventoryMapper.deleteById(inventoryId) > 0;
    }

    /**
     * 批量修改
     * @param list
     * @return
     */
    @Override
    public int updateBatch(List<HqInventory> list) {
        return this.hqInventoryMapper.updateBatch(list);
    }

    @Override
    public HqInventory selectDrugId(String drugId) {
        return this.hqInventoryMapper.queryByDrugId(drugId);
    }


    /**
     * 质检入库
     * @param list
     * @return
     */
    @Override
    public int quality(List<HqInventory> list) {
        return this.hqInventoryMapper.quality(list);
    }
}